package cn.wolfcode.web.controller;

import cn.wolfcode.annotation.PermissionName;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;


/**
 * ClassName PermissionController
 * Create by Mike
 * Date 2021/8/1 16:52
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @PermissionName("权限查询")
    @RequiresPermissions("permission:list")
    public String list(Model model, QueryObject qo){
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/permission/list";
    }

    @Autowired
    private RequestMappingHandlerMapping rmhm;

    @ResponseBody
    @RequestMapping("/load")
    public JsonResult load(){
        //权限加载代码
        try {
            //到数据库中查询所有的权限数据
            List<Permission> alreadyPermissions = permissionService.listAll();
            //用Set因为有序且去重
            Set<Permission> permissions = new LinkedHashSet<>();
            //获取Spring容器中的控制器对象
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
            //获取Map中的所有value
            Collection<HandlerMethod> methods = handlerMethods.values();
            for (HandlerMethod method : methods) {
                    RequiresPermissions annotation = method.getMethodAnnotation(RequiresPermissions.class);
                    if (annotation != null){
                        String expression = annotation.value()[0];
                        PermissionName annotation1 = method.getMethodAnnotation(PermissionName.class);
                        if (annotation1 != null){
                            String name = annotation1.value();
                            Permission permission = new Permission(name,expression);
                            if (!alreadyPermissions.contains(permission)){
                                permissions.add(permission);
                            }
                        }
                    }
            }
            //接着才往数据库中批量插入
            if (permissions.size() > 0){
                permissionService.batchSave(permissions);
            }
        }catch (Exception e){
            return new JsonResult(false,"权限加载失败");
        }
        return new JsonResult(true,"权限加载成功");
    }


    //访问没有权限的
    @RequestMapping("/noPermission")
    public String noPermission(){
        return "common/nopermission";
    }
}
